package Pages;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import Others.ButtonTabComponent;
import TableAdditions.RouteAdditionMenu;
import Tables.CustomerUnitPriceTableInterface;
import Tables.DriverRouteNameTableInterface;
import Tables.RouteNameRouteTableInterface;


public class RoutePageInterface extends JFrame implements ActionListener {
	public static int tabNum=0;
	JTabbedPane jtp;
	JPanel jp1;
	
	JLabel picturePicker = new JLabel();// MBS catch the image
	ImageIcon Resim1 = new ImageIcon(getClass().getResource("Rut.png"));
	
	JPanel[] panel;
	
	
	//JButton driverButton = new JButton("Rut Bilgileri");
    //JButton routeNameRouteButton = new JButton("RutName-Rut");
    JButton addRoute = new JButton("Rut ekleme");
    ///////////////////////////  for Time
    public JLabel show_time = new JLabel("Show Time");
    public JLabel Show_date = new JLabel("Show Date");

    DateFormat dateFormat2 = new SimpleDateFormat("h:mm:ss a");
    java.util.Date date2;
    long msec;
	//////////////////////////////
    
 
    DriverRouteNameTableInterface driverRouteNameTable;
    RouteNameRouteTableInterface routeNameRouteTable;
    CustomerUnitPriceTableInterface customerUnitPriceTable;
    
    
    public RoutePageInterface() {
    	this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    	tabNum=0;
    	
        jtp = new JTabbedPane(JTabbedPane.TOP);
        //This creates the template on the windowed application that we will be using
        getContentPane().add(jtp);
        jp1 = new JPanel();//This will create the first tab
        
        
        JPanel panel1 = new JPanel();
        JPanel panel[]=new JPanel[60];
        for(int i=0;i<60;i++)
    		panel[i]=new JPanel();
        panel1.setLayout(new GridBagLayout());
        
        picturePicker.setIcon(Resim1);
        addItem(panel1, picturePicker, 0, 0, 5, 2, GridBagConstraints.NORTH);
        
        Font f= new Font("SansSerif", Font.PLAIN, 13);
        DateFormat dateFormat = new SimpleDateFormat("EEEEEEEEE: dd MMMMMMMMM, yyyy.");
        java.util.Date date = new java.util.Date();
        String dateTime = dateFormat.format(date);
        Show_date = new JLabel(dateTime);
        Show_date.setFont(f); 
        show_time= new JLabel("");
        updateDateTime();
        show_time.setFont(f);
        
        Box showDateTimeBox = Box.createVerticalBox();
        JLabel showDateTimeGroup = new JLabel();
        showDateTimeGroup.add(Show_date);
        showDateTimeGroup.add(show_time);
        showDateTimeBox.add(Show_date);
        showDateTimeBox.add(show_time);
        showDateTimeBox.setBorder(BorderFactory.createTitledBorder(""));
        addItem(panel1, showDateTimeBox, 0, 3, 1, 1, GridBagConstraints.EAST);
        
       // driverButton.setPreferredSize(new Dimension(170,40));
        //routeNameRouteButton.setPreferredSize(new Dimension(170,40));
        addRoute.setPreferredSize(new Dimension(170,40));
        
        //driverButton.addActionListener(this);
        //routeNameRouteButton.addActionListener(this);
        addRoute.addActionListener(this);
        
        //addItem(panel1,driverButton,0,4,1,1,GridBagConstraints.EAST);
		//addItem(panel1,routeNameRouteButton,1,4,1,1,GridBagConstraints.EAST);
		addItem(panel1,addRoute,0,4,1,1,GridBagConstraints.EAST);
		
		for(int i=0;i<12;i++)
		{
			if(i<1){
			addItem(panel1,panel[i],3,i+4,1,1,GridBagConstraints.CENTER);
			addItem(panel1,panel[i+10],4,i+4,1,1,GridBagConstraints.CENTER);
			//addItem(panel1,panel[i+20],5,i+4,1,1,GridBagConstraints.CENTER);
			}else{
				addItem(panel1,panel[i],0,i+4,1,1,GridBagConstraints.CENTER);
				addItem(panel1,panel[i+12],1,i+4,1,1,GridBagConstraints.CENTER);
				addItem(panel1,panel[i+24],2,i+4,1,1,GridBagConstraints.CENTER);
				addItem(panel1,panel[i+36],3,i+4,1,1,GridBagConstraints.CENTER);
				addItem(panel1,panel[i+48],4,i+4,1,1,GridBagConstraints.CENTER);
			}
		}
        
		//Main tmp=new Main();

        jp1.add(panel1);
        //and adds the panel to the first tab

       //This adds the first to our tabbed pane object and names it
       jtp.addTab("Soför Rut Bilgileri", jp1); 
      // initTabComponent(0);
        
        //this.setMinimumSize(new Dimension(400,750));
        //this.setLocation(tmp.upperLeftX,tmp.upperLeftY);
       // this.setContentPane(panel1);
     //   this.pack();
        this.setVisible(true);
        
	}
    
    private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align) {
        GridBagConstraints gc = new GridBagConstraints();
        gc.gridx = x;
        gc.gridy = y;
        gc.gridwidth = width;
        gc.gridheight = height;
        gc.weightx = 100.0;
        gc.weighty = 100.0;
        gc.insets = new Insets(5, 5, 5, 5);
        gc.anchor = align;
        gc.fill = GridBagConstraints.NONE;
        p.add(c, gc);
      }
	private void updateDateTime()
    {
    Thread th = new Thread(new Runnable()
        {
  
		public void run()
            {
            while(true)
                {
                date2 = new java.util.Date();
                msec = date2.getTime();
                //msec += (7.5)*60*60*1000L;
                date2.setTime(msec);
                String dateTime = dateFormat2.format(date2.getTime());  
                show_time.setText(dateTime);
                show_time.updateUI();
                }
            }
        });
    th.start();
    }


	@Override
	public void actionPerformed(ActionEvent e) {
		/*if(e.getSource()==driverButton){
			++tabNum;
			driverRouteNameTable=new DriverRouteNameTableInterface();
        	javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                	jtp.addTab("Soför RutName Tablosu", driverRouteNameTable);
                	jtp.setSelectedComponent(driverRouteNameTable);
               	 	initTabComponent(tabNum); 
                }
            });
		}
		else if(e.getSource()==routeNameRouteButton){
			++tabNum;
			routeNameRouteTable=new RouteNameRouteTableInterface();
        	javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                	jtp.addTab("RutName Rut Tablosu", routeNameRouteTable);
                	jtp.setSelectedComponent(routeNameRouteTable);
                	initTabComponent(tabNum);  
                }
            });*/
		 if(e.getSource()==addRoute){
        	javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                	try {
    					RouteAdditionMenu frame = new RouteAdditionMenu();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
                }
            });
		}

	}
	   public void runTest() {
	    	
	        //tabComponentsItem.setSelected(true);
	        jtp.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
	        //scrollLayoutItem.setSelected(false);
	        setSize(new Dimension(950, 750));
	        setLocationRelativeTo(null);
	        setVisible(true);
	    }
	   private void initTabComponent(int i) {
   		jtp.setTabComponentAt(i,new ButtonTabComponent(jtp,2));
   }  

}
